package cn.edu.bjut.homework.chapter7;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class BoxLayoutTester {
	private JFrame frm;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					BoxLayoutTester window = new BoxLayoutTester();
					window.frm.setVisible(true);
					window.frm.pack();
					window.frm.setLocationRelativeTo(null);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public BoxLayoutTester() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frm = new JFrame();
		frm.setTitle("BoxLayout容器测试");
		frm.setBounds(100, 100, 450, 300);
		frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frm.getContentPane().setLayout(new BorderLayout(0, 0));

		JPanel topPanel = createTopPanel();
		frm.getContentPane().add(topPanel, BorderLayout.CENTER);

		JPanel bottomPanel = createBottomPanel();
		frm.getContentPane().add(bottomPanel, BorderLayout.SOUTH);
	}

	private JPanel createTopPanel() {
		String[] columnName = { "姓名", "性别", "单位", "参加项目", "备注" };
		String[][] rowData = { { "高明", "男", "计算机学院", "100米，铅球", "" }, 
				{ "白雪", "女", "经管学院", "100米，200米", "" },
				{ "高大上", "男", "人文学院", "1000米", "" } };

		JTable table = new JTable(new DefaultTableModel(rowData, columnName));
		JScrollPane scrollPane = new JScrollPane(table);
		scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);

		JPanel topPanel = new JPanel();
		topPanel.setLayout(new BoxLayout(topPanel, BoxLayout.Y_AXIS));
		topPanel.add(Box.createVerticalStrut(10));
		topPanel.add(scrollPane);
		topPanel.add(Box.createVerticalStrut(10));

		return topPanel;
	}

	private JPanel createBottomPanel() {
		JButton okButton = new JButton("确定");
		JButton closeButton = new JButton("关闭");

		JPanel buttonPanel = new JPanel();
		buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS));
		buttonPanel.add(okButton);
		buttonPanel.add(Box.createHorizontalGlue());
		buttonPanel.add(closeButton);

		JPanel bottomPanel = new JPanel();
		bottomPanel.setLayout(new BoxLayout(bottomPanel, BoxLayout.Y_AXIS));
		bottomPanel.add(Box.createVerticalStrut(10));
		bottomPanel.add(buttonPanel);
		bottomPanel.add(Box.createVerticalStrut(10));

		return bottomPanel;
	}
}